[% setvar title Replace AUTOLOAD by a more flexible mechanism %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Replace AUTOLOAD by a more flexible mechanism</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Ilya Zakharevich &lt;<a href='mailto:ilya@math.ohio-state.edu'>ilya@math.ohio-state.edu</a>&gt;
  Date: 15 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 232
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>This RFC proposes two-stage autoloading: one stage may be registered
to act when the symbol is encountered at compile time, the other
when the subroutine is called.  Autoloading on the second stage does not
<i>call</i> the subroutine, only <i>loads</i> it.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>AUTOLOAD interface was created before closures were available.  Because of
this, it combines two actions into one call: it loads the subroutine,
and calls it.</p>
<p>This two actions should be separated.  Additionally, some action may be needed
during compile time.  For example, one may fetch the prototype of the function,
or even call the function, so that its (constant) value may be used in this
and other calls.</p>
<p>[WIth the current implementation <i>all</i> the prototypes should be loaded no
matter what, with significant memory and time overheads.]</p>
<p>When a symbol for an undefined subroutine is seen at compile time, the
<code>proto</code> action is invoked.  It should return <code>undef</code> if no such symbol
could be autoloaded (so that the symbol may be searched further in the @ISA
tree).  It should return the prototype if the symbol could be found.  This
call may have arbitrary side effects, such as defining the subroutine, or even
calling it (useful for constant XSUBs).</p>
<p>When an undefined subroutine is about to be called, the <code>load</code> action is
invoked.  It should return a reference to a subroutine, or <code>undef</code> to signal
that search should be continued further in the @ISA tree.</p>
<p>In both cases, the argument for the action is the name of the subroutine.
If the subroutine has two different names (it was created with one name, and
was imported with a different name), both names are provided.</p>
<p>The <code>proto</code> and <code>load</code> subroutines may be registered (both or any one) with
the pragma</p>
<pre>  use autoload ':proto' =&gt; sub {}, ':load' =&gt; sub {};</pre>
<a name='MIGRATION ISSUES'></a><h1>MIGRATION ISSUES</h1>
<p>None.</p>
<p>[The current AUTOLOAD mechanism saves creation of a subroutine frame, since
the actual call to the subroutine may be done via <code>goto</code>.  If this is proved
to provide some advantage, AUTOLOAD mechanism may be preserved as well.]</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Straightforward.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>None.</p>
</div>
